// packageA/pages/partOutEdit/partOutEdit.js
const app = getApp();
import CommonService from '../../../utils/service/commonService.js';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
const util = require('../../../utils/util');
import Base64 from '../../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: { 
    //修改信息回显
    editInfo:{},
    //提交按钮禁用
    disabled:false,
    //避免表单重复提交
    code:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      code:Base64.encode(new Date() + app.globalData.userInfo.userName)
    })

    if(options.paramObj){
      let paramObj=JSON.parse(options.paramObj);
      console.log('配件出库编辑-传参');
      console.log(paramObj);

      if(paramObj.id){
        //获取配件出库信息
        this.getInfo(paramObj.id);
      }
    }  
  },

  //获取配件出库信息
  getInfo(id){
    let param = {
      isAuth: false,
      className: 'outStorageOrder',
      data: {
        id: id,
        selectFields: ['id', 'orderNo', 'client', 'outDate', 'outParts', 'totalPrice', 'keepAccounts', 'keepers', 'checking','consignee', 'prepared', 'other', 'remit', 'invoice', 'remark', 'type', 'outStorageType']
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];

        console.log('配件出库信息');
        console.log(result);
        
        //出库时间
        if(result.outDate){
          result.outDate=util.timeFormat(result.outDate);
        }

        //采购时间
        if(result.other && result.other[0].purchaseDate){
          result.other[0].purchaseDate=util.timeFormat(result.other[0].purchaseDate);
        } 
        
        //是否付款
        result.remit=result.remit+'';

        //是否开票
        result.invoice=result.invoice+'';
        
        this.setData({
          editInfo: result
        })
      }
    }).catch((error) => {
    })
  },

  //是否已付款
  onChangeRemit(e) {  
    let remit='editInfo.remit';
    this.setData({
      [remit]: e.detail,
    })
  },

  //是否已开票
  onChangeInvoice(e) {  
    let invoice='editInfo.invoice';
    this.setData({
      [invoice]: e.detail,
    })
  },

  //提交表单
  confirmForm(e){
    this.setData({
      disabled:true
    })

    let obj=e.detail.value;
    let td=this.data;
    let editInfo=td.editInfo;

    let param={
      className:'outStorageOrder',
      data:{
        requestToken: td.code,
        id: editInfo.id,
        remit: Boolean(editInfo.remit),
        invoice: Boolean(editInfo.invoice),
        remark: obj.remark.trim()
      }
    }

    //避免表单重复提交
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '数据提交中...'
    });

    //console.log(param.data);

    CommonService.goSave(param).then(res=>{
      this.setData({
        disabled:false
      })
      if (res.statusCode == 200 && res.data.success == true){
        Toast.success('成功');
        setTimeout(() => {
          //返回列表页并刷新数据
          this.backAndRefresh();
        }, 1000);
      }else if(res.statusCode==202){
        Toast.fail('您操作太快了,请勿重复提交');
      }else{
        Toast.fail('操作失败');
      }
    }).catch((error) => {
      Toast.fail('操作失败');
    }) 
  },

  //返回列表页并刷新数据
  backAndRefresh(){
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];

    prevPage.refreshListAndPage();
    wx.navigateBack();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})